from typing import TYPE_CHECKING

from sqlalchemy import ForeignKey, JSON, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.db.base_class import BaseEntity

if TYPE_CHECKING:
  from .relationship import Relationship
  from .user import User


class AISession(BaseEntity):
  __tablename__ = "ai_sessions"

  user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
  relationship_id: Mapped[int | None] = mapped_column(ForeignKey("relationships.id"), index=True)
  thread_id: Mapped[str] = mapped_column(String(80), index=True)
  context_json: Mapped[dict] = mapped_column(JSON, default=dict)

  user: Mapped["User"] = relationship(back_populates="ai_sessions", lazy="joined")
  relationship: Mapped["Relationship | None"] = relationship("Relationship", lazy="joined")
